package org.jackysoft.controller;

import java.util.Collection;
import java.util.HashMap;

import javax.annotation.Resource;

import org.jackysoft.controller.base.AbstractController;
import org.jackysoft.entity.ActionNode;
import org.jackysoft.entity.InitialActionsNode;
import org.jackysoft.entity.OsTask;
import org.jackysoft.entity.User;
import org.jackysoft.entity.WorkflowNode;
import org.jackysoft.service.OsVariableService;
import org.jackysoft.service.WorkflowNodeService;
import org.jackysoft.util.WorkflowConstant;
import org.jackysoft.workflow.Workflow;
import org.jackysoft.workflow.WorkflowException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.base.Strings;

/**
 * <dd>根据工作流名称,初始化一个工作流 工作流名称 从</dd> <dd>上一个页面传递过来 实例变量</dd> <dd>这个类只
 * 初始化一个工作流实例然后把实例的</dd> <dd>所代表的工作流id 放到session中 去就返回</dd>
 * 
 * @author 屈甲康 {@link qujiakang@gmail.com}
 * 
 */

@Controller
@RequestMapping("/oswf/init")
public class StartWorkController extends AbstractController<OsTask> {
	
	@Resource
	WorkflowNodeService nodeService;
	
	@Resource
	OsVariableService variableService;
	
	@Resource
	Workflow workflow;
	
		
	@RequestMapping("/execute")
	public String execute(
			String refname,
			int actionId,
			long workflowId
			) throws Exception {

		if (Strings.isNullOrEmpty(refname))
			throw new WorkflowException("初始化工作流失败,原因:工作流定义 不能为null ");
		User user = null;
		//TODO 获取当前用户
		log.info(String.format("%s启动工作流 [%s]", user.getUsername(), refname));
		final InitialActionsNode initact = nodeService.getInitialActionsNode(refname);
		if (initact == null)
			throw new WorkflowException("初始化工作流失败,原因:初始化活动不存在  ");
		final ActionNode initAction = initact.getActions().iterator().next();
		if (initAction == null)
			throw new WorkflowException("初始化工作流失败,原因:初始化活动不存在  ");
		actionId = initAction.getId();
		Workflow.ACTION_ID.set(actionId);
		workflowId = workflow.initialize(refname, actionId,new HashMap<String,Object>());
		Workflow.WORKFLOW_ID.set(workflowId);	
			
		variableService
				.setVariable(workflowId, WorkflowConstant.ACTION_ID.toString(),
						String.valueOf(actionId));
		variableService.setVariable(workflowId,
				WorkflowConstant.ACTION_NAME.toString(), initAction.getName());
		variableService.setVariable(workflowId,
				WorkflowConstant.STARTER.toString(), user.getLocalName());
		variableService.setVariable(workflowId,
				WorkflowConstant.STARTER_USERID.toString(), user.getUsername());		
		variableService.setVariable(workflowId,
				WorkflowConstant.LAST_EXECUTOR.toString(), user.getLocalName());
		variableService.setVariable(workflowId,
				WorkflowConstant.LAST_EXECUTOR_USERID.toString(),
				user.getUsername());
		Workflow.LOCAL_STATUS.set(WorkflowConstant.STATE_UNDERWAY);
		return "selectdoc";

	}


	@RequestMapping("/start")
	public void start(@ModelAttribute Collection<WorkflowNode> beans) {
		beans = nodeService.getSimples();
		
	}

}